import { boot } from "quasar/wrappers";
import axios from "axios";

// Be careful when using SSR for cross-request state pollution
// due to creating a Singleton instance here;
// If any client changes this (global) instance, it might be a
// good idea to move this instance creation inside of the
// "export default () => {}" function below (which runs individually
// for each client)
const api = axios.create({ baseURL: "https://api.zhuchundashuaige.com" });

// 搜索API服务
export const searchAPI = {
  // 搜索音乐 (search_type=1)
  searchMusic: async (keyword) => {
    try {
      const response = await api.get("/api/v1/search", {
        params: {
          search_type: 1,
          search_keyword: keyword,
        },
        headers: {
          ApiKey: btoa(
            `${localStorage.getItem("token") || ""}` +
              "$$" +
              `${localStorage.getItem("email") || ""}`
          ),
        },
      });
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  // 搜索视频 (search_type=0)
  searchVideo: async (keyword) => {
    try {
      const response = await api.get("/api/v1/search", {
        params: {
          search_type: 0,
          search_keyword: keyword,
        },
        headers: {
          ApiKey: btoa(
            `${localStorage.getItem("token") || ""}` +
              "$$" +
              `${localStorage.getItem("email") || ""}`
          ),
        },
      });
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  // 获取音乐详情
  getMusicDetail: async (searchKeyword, id) => {
    try {
      const response = await api.get("/api/v1/search", {
        params: {
          search_type: 1,
          search_keyword: searchKeyword,
          id: id,
        },
        headers: {
          ApiKey: btoa(
            `${localStorage.getItem("token") || ""}` +
              "$$" +
              `${localStorage.getItem("email") || ""}`
          ),
        },
      });
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  // 搜索小说 (search_type=2)
  searchBook: async (keyword) => {
    try {
      const response = await api.get("/api/v1/search", {
        params: {
          search_type: 2,
          search_keyword: keyword,
        },
        headers: {
          ApiKey: btoa(
            `${localStorage.getItem("token") || ""}` +
              "$$" +
              `${localStorage.getItem("email") || ""}`
          ),
        },
      });
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  // 获取小说详情
  getBookDetail: async (searchKeyword, id) => {
    try {
      const response = await api.get("/api/v1/search", {
        params: {
          search_type: 2,
          search_keyword: searchKeyword,
          id: id,
        },
        headers: {
          ApiKey: btoa(
            `${localStorage.getItem("token") || ""}` +
              "$$" +
              `${localStorage.getItem("email") || ""}`
          ),
        },
      });
      return response.data;
    } catch (error) {
      throw error;
    }
  },
};

export default boot(({ app }) => {
  // for use inside Vue files (Options API) through this.$axios and this.$api

  app.config.globalProperties.$axios = axios;
  // ^ ^ ^ this will allow you to use this.$axios (for Vue Options API form)
  //       so you won't necessarily have to import axios in each vue file

  app.config.globalProperties.$api = api;
  // ^ ^ ^ this will allow you to use this.$api (for Vue Options API form)
  //       so you can easily perform requests against your app's API

  app.config.globalProperties.$searchAPI = searchAPI;
});

export { api };
